package com.school.aftersalemanagement.service.impl;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.school.aftersalemanagement.callback.StringCallback;
import com.school.aftersalemanagement.callback.impl.GetBikeCallback;
import com.school.aftersalemanagement.callback.impl.RecordCallback;
import com.school.aftersalemanagement.callback.impl.ResultCallback;
import com.school.aftersalemanagement.callback.impl.getFenceCallback;
import com.school.aftersalemanagement.entity.Result;
import com.school.aftersalemanagement.global.Apis;
import com.school.aftersalemanagement.service.BicycleService;
import com.school.aftersalemanagement.service.Service;
import com.zhy.http.okhttp.request.RequestCall;


/**
 * Created by winkyle on 2017/11/8.
 */

public class BicycleServiceimpl extends Service implements BicycleService{
    public BicycleServiceimpl(Context context, Handler handler) {
        super(context, handler);
    }

    @Override
    public void getBicycleList(int MessageType,String locations,int page) {
        map.clear();
        map.put("locations",locations);
        map.put("page",page+"");
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.CheckBike,map);
        requestCall.execute(new GetBikeCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void mainTenance(int MessageType,String bike_number) {
        map.clear();
        map.put("bike_number",bike_number);
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.mainTenance,map);
        requestCall.execute(new ResultCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void setRemark(int MessageType, String number, String remark) {
        map.clear();
        map.put("number",number);
        map.put("remark",remark);
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.setRemark,map);
        requestCall.execute(new ResultCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void serviceUnlock(int MessageType, String bike_number) {
        map.clear();
        map.put("bike_number",bike_number);
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.serviceUnlock,map);
        requestCall.execute(new ResultCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void solveProblem(int MessageType, String bike_number) {
        map.clear();
        map.put("bike_number",bike_number);
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.solveProblem,map);
        requestCall.execute(new ResultCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void lockBike(int MessageType, String bike_number,int lock) {
        map.clear();
        map.put("bike_number",bike_number);
        map.put("lock",lock+"");
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.lockBike,map);
        requestCall.execute(new ResultCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void repairList(int MessageType) {
        map.clear();
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.repairList,map);
        requestCall.execute(new GetBikeCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void setWarnTimes(int MessageType, String warn_times) {
        map.clear();
        map.put("warn_times",warn_times);
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.setWarnTimes,map);
        requestCall.execute(new ResultCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void searchMaintance(int MessageType) {
        map.clear();
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.searchMaintance,map);
        requestCall.execute(new RecordCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void getFence(int MessageType) {
        map.clear();
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.getFence,map);
        requestCall.execute(new getFenceCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void searchBike(int MessageType, String number, String lease_status, String fence_id, String remark_status, String sort_riding_times, String sort_repaired_rtimes,String pageNumber) {
        map.clear();
        map.put("number",number);
        if(lease_status!=null)
        map.put("lease_status",lease_status);
        map.put("fence_id",fence_id);
        if(remark_status!=null)
        map.put("remark_status",remark_status);
        map.put("sort_riding_times",sort_riding_times);
        map.put("sort_repaired_rtimes",sort_repaired_rtimes);
        map.put("pageNumber",pageNumber);
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.searchBike,map);
        requestCall.execute(new GetBikeCallback(requestCall,handler,MessageType,context,true));
    }

    @Override
    public void getRemindingTimes(int MessageType) {
        map.clear();
        RequestCall requestCall = getRequestCall(Apis.Base + Apis.getRemindingTimes,map);
        requestCall.execute(new StringCallback(requestCall, handler, MessageType, context, false) {

            @Override
            public void onResponse(String response, int id) {
                Result result = getResult(response);
                if(result.getBody()!=null)
                handler.obtainMessage(messageType,result.getBody().toString()).sendToTarget();
            }
        });
    }


}
